// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command


/** 云函数功能：
 *  1. 更改当前券的库存
    exchange_log = {
      openid: openid,
      ticketid: ticketid, // 兑换卡券id
      ticket_name: that.tickets[ticketid].value,
      consume_likes: that.tickets[ticketid].value,
      remark: '兑换[' + ticket_name + '] , 消耗 [' + consume_likes +']'
    }
    2. 更新用户信息

    3. 添加核销记录
 */

// 云函数入口函数
exports.main = async (event, context) => {

  let openid = event.openid
  let ticketid = Number(event.ticketid)

  // ******* 查询 tickets 数据 *******
  let get_tickets = await db.collection('activity_settings').doc('5cf7770f47882f40e4a3ceaa').get()
  if (get_tickets.errMsg !== 'document.get:ok' || !get_tickets.data) {
    return { result: 'fail', error: '网路错误', more: '读取活动settigns失败' }
  }
  let tickets = get_tickets.data.tickets
  tickets[ticketid].total_count -= 1

  // 当前券所需赞数
  let ticket_need_likes = Number(tickets[ticketid].need_likes)
  console.log(ticket_need_likes)

  // ******* 对 ticket 对应的券值 total_count -1 操作  *******

  let update_tickets = await db.collection('activity_settings').doc('5cf7770f47882f40e4a3ceaa').update({
    data: {
      tickets: tickets
    }
  })
  if (update_tickets.errMsg !== 'document.update:ok' || update_tickets.stats.updated !== 1) {
    return { result: 'fail', error: '网路错误', more: 'tickets总数-1修改失败' }
  }

  // ******* 查询当前用户信息  *******
  let userinfo = await db.collection('activity_users').doc(openid).get()
  if (userinfo.errMsg !== 'document.get:ok' || !userinfo.data) {
    return { result: 'fail', error: '网路错误', more: '读取用户信息失败' }
  }
  let user = userinfo.data
  let new_current_likes = Number(user.current_likes) - Number(ticket_need_likes)
  let new_consume_likes = Number(user.consume_likes) + Number(ticket_need_likes)

  let add_user_record = await db.collection('activity_users').doc(openid).update({
    data: {
      current_likes: new_current_likes,
      consume_likes: new_consume_likes,
      exchanged_ticketid: _.unshift([ticketid])
    }
  })
  if (add_user_record.errMsg !== 'document.update:ok' || add_user_record.stats.updated !== 1) {
    return { result: 'fail', error: '网路错误', more: '用户列表添加兑换记录失败' }
  }

  // ******* 添加完整兑换记录  *******

  let add_record = await db.collection('activity_exchange_record').add({
    data:{
      openid: openid,
      user_avatar_url: user.user_avatar_url,
      user_nick_name: user.user_nick_name,
      ticketid: ticketid, // 兑换卡券id
      ticket_name: tickets[ticketid].name,
      ticket_value: tickets[ticketid].value,
      consume_likes: ticket_need_likes,
      remark: '兑换[' + tickets[ticketid].name + '] , 消耗 [' + ticket_need_likes + ']'
    }
  })

  // ******* 运行至此，说明无误  *******
  user.current_likes = new_current_likes
  user.consume_likes = new_consume_likes
  if(user.exchanged_ticketid){
    user.exchanged_ticketid.unshift(ticketid)
  }else{
    user.exchanged_ticketid = [ticketid]
  }

  return {
    result: 'success',
    current_user_activity_info: user,
    tickets: tickets
  }
}